package com.elevator;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import data.setting;

interface simulationBtnListener
{
	void runBtnListener(int run);
}
public class simulationSettingUI extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JComboBox mProbobilityBox = new JComboBox();
	public JComboBox mPreTimeBox = new JComboBox();
    public JTextField mFileNameField = new JTextField();
    public JButton mRunBtn = new JButton("Run");
    public JButton mRunSaveBtn = new JButton("Run&Save As");
    public JButton mRunFromResult = new JButton("Run Store Data");
    public JComboBox mHistoryRecordBox = new JComboBox();
    public Vector<ButtonGroup> mLayerButtonGroups = new Vector<ButtonGroup>();
    public Vector<JRadioButton> mRadioButtons = new Vector<JRadioButton>();
    public int mToLayer[] = null;
    public int mFromLayer[] = null;
    public JButton mBackBtn = new JButton("Back");
    public simulationSettingUI()
    {
        this.setLayout(null);
        JLabel label = new JLabel("ProbobilityBox:");
        label.setLocation(50, 50);
        label.setSize(100, 20);
        this.add(label);
        
        label = new JLabel("Setting");
        label.setLocation(10, 10);
        label.setSize(100, 20);
        this.add(label);
        
        this.add(mProbobilityBox);
        mProbobilityBox.setLocation(50, 70);
        mProbobilityBox.setSize(100, 20);
        
        mProbobilityBox.addItem("1/6");
        mProbobilityBox.addItem("2/6");
        mProbobilityBox.addItem("3/6");
        mProbobilityBox.addItem("4/6");
        mProbobilityBox.addItem("5/6");
        mProbobilityBox.addItem("6/6");
        
        mProbobilityBox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				int nIndex = mProbobilityBox.getSelectedIndex();
				 for (int i = 0; i < mLayerButtonGroups.size(); i++) {
			        	ButtonGroup group = mLayerButtonGroups.get(i);
			        	 for ( Enumeration<AbstractButton> e = group.getElements(); e.hasMoreElements();)
			        	 {
								JRadioButton btn = (JRadioButton)e.nextElement();
								if (i<=nIndex) {
									btn.setEnabled(true);
								}
								else 
								{
									btn.setEnabled(false);
								}
			        	 }
				 }
			}
		});
        label = new JLabel("Pre Cycle");
        label.setLocation(100, 100);
        label.setSize(100, 40);
        this.add(label);
        
        this.add(mPreTimeBox);
        mPreTimeBox.addItem("slow");
        mPreTimeBox.addItem("Fast");
        mPreTimeBox.setLocation(50, 110);
        mPreTimeBox.setSize(50, 25);
        
        
        mRunBtn.setLocation(700, 60);
        mRunBtn.setSize(100, 30);
        mRunBtn.setMargin(new Insets(0, 0, 0, 0));
        this.add(mRunBtn);
        
        mRunSaveBtn.setLocation(700, 110);
        mRunSaveBtn.setSize(80, 30);
        mRunSaveBtn.setMargin(new Insets(0, 0, 0, 0));
        this.add(mRunSaveBtn);
        
        mFileNameField.setLocation(785, 110);
        mFileNameField.setSize(100, 30);
        this.add(mFileNameField);
        
        
        mRunFromResult.setLocation(780, 160);
        mRunFromResult.setSize(100, 30);
        mRunFromResult.setMargin(new Insets(0, 0, 0, 0));
        this.add(mRunFromResult);
        
        mHistoryRecordBox.setLocation(700, 165);
        mHistoryRecordBox.setSize(70, 20);
        this.add(mHistoryRecordBox);
        
        label = new JLabel("floor NO");
        label.setLocation(230, 160);
        label.setSize(100, 40);
        this.add(label);
       
        
        label = new JLabel("Person Id");
        label.setLocation(150, 220);
        label.setSize(80, 40);
        this.add(label);
        
        for (int i = 0; i < setting.sLayerCount; i++) {
        	label = new JLabel(""+(i+1));
            label.setLocation(250+i*30, 200);
            label.setSize(30, 30);
            this.add(label);;
		}
        
        for (int i = 0; i < 6; i++) {
        	label = new JLabel(""+(i+1));
            label.setLocation(215, 225+i*30);
            label.setSize(30, 30);
            this.add(label);;
		}
        
        for (int i = 0; i < 6; i++) {
        	ButtonGroup group = new ButtonGroup();
        	
			for (int j = 0; j < setting.sLayerCount ; j++) {
				JRadioButton box = new JRadioButton();
				box.setLocation(245+j*30, 230+i*30);
				box.setSize(25, 25);
				group.add(box);
				mRadioButtons.add(box);
				box.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						int nIndex = mRadioButtons.indexOf(arg0.getSource());
						int nRow = nIndex / setting.sLayerCount;
						int nColumn = nIndex % setting.sLayerCount;
						
						Random random = new Random();
						int nLayerId = random.nextInt(setting.sLayerCount);
						if (nLayerId == nColumn) {
							nLayerId = (nLayerId +1)%setting.sLayerCount;
						}
						mToLayer[nRow] = nLayerId;
						mFromLayer[nRow] = nColumn;

					}
				});
				if(i> mProbobilityBox.getSelectedIndex())
				{
					box.setEnabled(false);
				}
				this.add(box);
			}
			mLayerButtonGroups.add(group);
		}
        
        mToLayer = new int[6];
        mFromLayer= new int[6];
        for (int i = 0; i < mToLayer.length; i++) {
			mToLayer[i] = -1;
		}
        for (int i = 0; i < mToLayer.length; i++) {
        	mFromLayer[i] = -1;
		}
        
        this.add(mBackBtn);
        mBackBtn.setBounds(120, 10, 80, 30);
        
        getHistoryRecord();
    }
    public void resetUI()
    {
        for ( int i = 0;i<this.mRadioButtons.size();i++)
        {
            this.remove(mRadioButtons.get(i));
        }
        mRadioButtons.clear();
        this.mLayerButtonGroups.clear();
        for (int i = 0; i < 6; i++) {
        	ButtonGroup group = new ButtonGroup();

			for (int j = 0; j < setting.sLayerCount ; j++) {
				JRadioButton box = new JRadioButton();
				box.setLocation(245+j*30, 230+i*30);
				box.setSize(25, 25);
				group.add(box);
				mRadioButtons.add(box);
                                if(j == this.mFromLayer[i])
                                {
                                    box.doClick();
                                }
				box.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						int nIndex = mRadioButtons.indexOf(arg0.getSource());
						int nRow = nIndex / setting.sLayerCount;
						int nColumn = nIndex % setting.sLayerCount;

						Random random = new Random();
						int nLayerId = random.nextInt(setting.sLayerCount);
						if (nLayerId == nColumn) {
							nLayerId = (nLayerId +1)%setting.sLayerCount;
						}
						mToLayer[nRow] = nLayerId;
						mFromLayer[nRow] = nColumn;

					}
				});
				if(i> mProbobilityBox.getSelectedIndex())
				{
					box.setEnabled(false);
				}
				this.add(box);
			}
			mLayerButtonGroups.add(group);
		}

    }
    public void getHistoryRecord()
    {
    	File file=new File("elevatordb");
    	if (!file.exists()) {
			file.mkdir();
		}
    	File filelist[] = file.listFiles();
    	mHistoryRecordBox.removeAllItems();
    	for (int i = 0; i < filelist.length; i++) {
			System.err.println(filelist[i].getName());
			mHistoryRecordBox.addItem(filelist[i].getName());
			
		}
    }
}
